<?php

namespace Modules\Taxonomy\Library;

use Phalcon\Di;

class Common
{
    public static function termList($element)
    {
        if (!isset($element['term'])) {
            return [];
        }
        $data = Di::getDefault()->getService('entityManager')->find('term', [
            'conditions' => [
                'contentModel' => $element['term']['taxonomy'],
                'parent' => '0'
            ]
        ]);

        return self::eachTermList($data);
    }

    public static function eachTermList($data,$start = ''){
        $output = [];
        foreach ($data as $item){
            $output[strtolower($item->getId())] = $start.$item->name;
            $children = $item->getChildren();
            if($children){
                $output += self::eachTermList($children,$start.'-');
            }
        }
        return $output;
    }
}